﻿
using System;
using System.Data;
using System.Reflection;
using System.Windows.Forms;
using CuteSprite.MIS.Configuration;
using CuteSprite.MIS.Localization;
using CuteSprite.MIS.Server.Configuration.Localization;
using HmFramework;
using HmFramework.Extension;
using HmFramework.IO;
using HmFramework.Text;
using HmFramework.Utils.WinForm;

namespace CuteSprite.MIS.Server.Configuration
{
	public partial class FrmUser : HmBaseForm
	{
		private readonly String m_emptyPwd = "0        ";

		#region -- 属性 --

		private AdminConfigInfo _UserSettings = null;

		internal AdminConfigInfo UserSettings
		{
			get { return _UserSettings; }
			set { _UserSettings = value as AdminConfigInfo; }
		}

		private DataRow _UserItem = null;

		internal DataRow UserItem
		{
			get { return _UserItem; }
			set
			{
				_UserItem = value as DataRow;
				txtUser.Text = _UserItem[AdminConfigInfo._.Column_User_Name].ToString();
				txtUser.ReadOnly = true;
				//txtPwd.Text = _UserItem[AdminConfigInfo._.Column_User_Password].ToString();
				txtPwd.Text = m_emptyPwd;
			}
		}

		private Boolean _IsAddNew = true;

		/// <summary>是否新增记录</summary>
		internal Boolean IsAddNew
		{
			get { return _IsAddNew; }
			set { _IsAddNew = value; }
		}

		#endregion

		#region -- 界面初始化 --

		public FrmUser()
		{
			InitializeComponent();
			InitForm();
		}

		public static FrmUser Create(AdminConfigInfo s)
		{
			FrmUser frm = new FrmUser();
			frm.IsAddNew = true;
			frm.UserSettings = s;
			return frm;
		}

		public static FrmUser Create(AdminConfigInfo s, DataRow dr)
		{
			FrmUser frm = new FrmUser();
			frm.IsAddNew = false;
			frm.UserSettings = s;
			frm.UserItem = dr;
			return frm;
		}

		#endregion

		#region -- Override Methods --

		#region - method InitIcon -

		/// <summary>载入窗体图标</summary>
		public override void InitIcon()
		{
			this.Icon = FileSource.GetIcon(Assembly.GetExecutingAssembly(), "csmis.ico");
		}

		#endregion

		#region - method LoadLocalization -

		/// <summary>载入语言包</summary>
		public override void LoadLocalization()
		{
			this.Text = MISConfigLocalizer.GetString(MISConfigStringId.FrmUser_Caption_From);
			btnOK.Text = MISCommonLocalizer.GetString(MISCommonStringId.Caption_btnOK);
			btnCancel.Text =MISCommonLocalizer.GetString(MISCommonStringId.Caption_btnCancel);
			gpUser.Text = MISConfigLocalizer.GetString(MISConfigStringId.FrmUser_Caption_gpUser);
			lblUser.Text = MISConfigLocalizer.GetString(MISConfigStringId.FrmUser_Caption_lblUser);
			lblPwd.Text = MISConfigLocalizer.GetString(MISConfigStringId.FrmUser_Caption_lblPwd);
		}

		#endregion

		#region - method CheckInput -

		public override bool CheckInput()
		{
			String user = txtUser.Text.Trim();
			if (MatchHelper.StrIsNullOrEmpty(user))
			{
				DialogHelper.ShowWarning(this.Text, MISConfigLocalizer.GetString(MISConfigStringId.Message_UserEmpty));
				return false;
			}
			if (MatchHelper.StrIsNullOrEmpty(txtPwd.Text))
			{
				DialogHelper.ShowWarning(this.Text, MISConfigLocalizer.GetString(MISConfigStringId.Message_PwdEmpty));
				return false;
			}
			if (IsAddNew)
			{
				try
				{
					var drs = UserSettings.Select(AdminConfigInfo._.Table_User, AdminConfigInfo._.Column_User_Name + " = '" + user + "'");
					if (drs != null && drs.Length > 0)
					{
						DialogHelper.ShowWarning(this.Text, MISConfigLocalizer.GetString(MISConfigStringId.Message_UserExists).FormatWith(user));
						return false;
					}
				}
				catch { }
			}
			return true;
		}

		#endregion

		#endregion

		#region -- Events Handling --

		private void btnOK_Click(object sender, EventArgs e)
		{
			Boolean flag = CheckInput();
			if (!flag) { return; }

			if (IsAddNew)
			{
				var tbIP = UserSettings.GetTable(AdminConfigInfo._.Table_User);
				DataRow dr = tbIP.NewRow();
				dr[AdminConfigInfo._.Column_User_ID] = Guid.NewGuid().ToString();
				dr[AdminConfigInfo._.Column_User_Name] = txtUser.Text.Trim();
				dr[AdminConfigInfo._.Column_User_Password] = StringHelper.EncryptPassword(txtPwd.Text.Trim());
				tbIP.Rows.Add(dr);
			}
			else
			{
				var pwd = txtPwd.Text;
				// 判断用户是否输入新的密码
				if (pwd != m_emptyPwd)
				{
					_UserItem[AdminConfigInfo._.Column_User_Password] = StringHelper.EncryptPassword(pwd.Trim());
				}
			}

			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		#endregion

	}
}
